package net.onceface.application.assembler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import net.onceface.application.dto.FeatureDTO;
import net.onceface.core.domain.Feature;
import net.onceface.core.domain.Product;
@SuppressWarnings("unchecked")
public class FeatureAssembler {

	public static Feature toEntity(FeatureDTO featureDTO){
		if(featureDTO == null){
			return null;
		}
		Feature result = new Feature();
		result.setId(featureDTO.getId());
		result.setDisabled(featureDTO.isDisabled());
		result.setCreateDate(featureDTO.getCreateDate());
		result.setPicture(featureDTO.getPicture());
		result.setVersion(featureDTO.getVersion());
		result.setAbolishDate(featureDTO.getAbolishDate());
		result.setName(featureDTO.getName());
		result.setSortOrder(featureDTO.getSortOrder());
		result.setStartTime(featureDTO.getStartTime());
		result.setProducts((Set<Product>) ProductAssembler.toEntities(featureDTO.getProducts()));
		if(featureDTO.getSecondDiscount() != null){
			result.setSecondDiscount(DisCountAssembler.toEntity(featureDTO.getSecondDiscount()));
		}
		if(featureDTO.getFirstDiscount() != null){
			result.setFirstDiscount(DisCountAssembler.toEntity(featureDTO.getFirstDiscount()));
		}
		return result;
	}
	
	public static FeatureDTO toDTO(Feature feature){
		if(feature == null){
			return null;
		}
		FeatureDTO result = new FeatureDTO();
		result.setId(feature.getId());
		result.setDisabled(feature.isDisabled());
		result.setCreateDate(feature.getCreateDate());
		result.setPicture(feature.getPicture());
		result.setVersion(feature.getVersion());
		result.setAbolishDate(feature.getAbolishDate());
		result.setName(feature.getName());
		result.setSortOrder(feature.getSortOrder());
		result.setStartTime(feature.getStartTime());
		
		if(feature.getFirstDiscount() != null){
			result.setFirstDiscount(DisCountAssembler.toDTO(feature.getFirstDiscount()));
		}
		if(feature.getSecondDiscount() != null){
			result.setSecondDiscount(DisCountAssembler.toDTO(feature.getSecondDiscount()));
		}
		return result;
	}
	public static List<FeatureDTO> toDTOs(Collection<Feature> feature){
		if(feature == null){
			return null;
		}
		List<FeatureDTO> results = new ArrayList<FeatureDTO>();
		for(Feature each : feature){
			results.add(toDTO(each));
		}
		return results;
	}
	
	public static List<Feature> toEntitys(Collection<FeatureDTO> featureDTO){
		if(featureDTO == null){
			return null;
		}
		List<Feature> results = new ArrayList<Feature>();
		for(FeatureDTO each : featureDTO){
			results.add(toEntity(each));
		}
		return results;
	}
}
